﻿using System;
using System.Web;
using Newtonsoft.Json;
using eCommerce.core.Helpers;
using eCommerce.db.BORespository;
using eCommerce.db.DataRespository;
using eCommerce.log;

namespace eCommerce.db.Action
{
    public class DiscountAction : IBaseAction
    {
        public void Do(HttpContext context)
        {
            var actionName = Utils.GetIdFromRequestString(Utils.Action, string.Empty);

            var responseData = new ResponseData();
            switch (actionName)
            {
                case "search":
                    responseData = ListSearch();
                    break;

                case "create":
                    responseData = Create();
                    break;
                case "update":
                    responseData = Update();
                    break;
                case "delete":
                    responseData = Delete();
                    break;

                case "":
                    responseData.Success = false;
                    responseData.Message = "Can not found this action.";
                    break;
            }

            var jSonString = JsonConvert.SerializeObject(responseData, Utils.SerializeFormat("MM/dd/yyyy HH:mm:ss"));
            if (!string.IsNullOrEmpty(context.Request.Params["callback"]))
                jSonString = string.Format("{0}({1})", context.Request.Params["callback"], jSonString);
            context.Response.Write(jSonString);
        }

        private static ResponseData ListSearch()
        {
            var responseData = new ResponseData();
                                          
            var startDate   = Utils.GetIdFromRequestString(Utils.StartDate, string.Empty);
            var endDate     = Utils.GetIdFromRequestString(Utils.EndDate, string.Empty);
            var pageIndex   = Utils.GetIdFromRequestString(Utils.EndDate, Constant.PageIndex);
            var pageSize    = Utils.GetIdFromRequestString(Utils.EndDate, Constant.PageSize);

            try
            {
                var totalRecords = 0;
                var lstDiscount  = DiscountBo.Search(startDate, endDate, pageIndex, pageSize, ref totalRecords);

                foreach (var discount in lstDiscount)
                {
                    discount.FromatStartDate = Utils.FormatIn_DDMMYYYY(discount.StartDate);
                    discount.FromatEndDate   = Utils.FormatIn_DDMMYYYY(discount.EndDate);
                }

                responseData.Success  = true;
                responseData.Data     = lstDiscount;
                responseData.TotalRow = totalRecords;                                
            }
            catch (Exception ex)
            {
                responseData.Success = false;
                responseData.Message = ex.Message;
                Logger.WriteLog(Logger.LogType.Error, ex.Message);
            }

            return responseData;
        }

        private static ResponseData Create()
        {
            var responseData = new ResponseData();

            var totalValue      = Utils.GetIdFromRequestString(Utils.TotalValue, 0);
            var discountPercent = Utils.GetIdFromRequestString(Utils.Percent, 0);
            var startDate       = Utils.GetIdFromRequestString(Utils.StartDate, string.Empty);
            var endDate         = Utils.GetIdFromRequestString(Utils.EndDate, string.Empty);

            try
            {
                var o = new DiscountEntity();
                o.TotalValue        = totalValue;
                o.DiscountPercent   = discountPercent;
                o.StartDate         = Utils.ToDateTime(Utils.ValidStrDateTime(startDate));
                o.EndDate           = Utils.ToDateTime(Utils.ValidStrDateTime(endDate));

                DiscountBo.Create(o);    
                
                responseData.Success  = true;
                responseData.Data     = null;
                responseData.TotalRow = 0;
            }
            catch (Exception ex)
            {
                responseData.Success = false;
                responseData.Message = ex.Message;
                Logger.WriteLog(Logger.LogType.Error, ex.Message);
            }
            return responseData;
        }

        private static ResponseData Update()
        {
            var responseData = new ResponseData();

            var id              = Utils.GetIdFromRequestString(Utils.DiscountId, 0);
            var totalValue      = Utils.GetIdFromRequestString(Utils.TotalValue, 0);
            var discountPercent = Utils.GetIdFromRequestString(Utils.Percent, 0);
            var startDate       = Utils.GetIdFromRequestString(Utils.StartDate, string.Empty);
            var endDate         = Utils.GetIdFromRequestString(Utils.EndDate, string.Empty);

            try
            {
                var o = new DiscountEntity();
                o.DiscountId        = id;
                o.TotalValue        = totalValue;
                o.DiscountPercent   = discountPercent;
                o.StartDate         = Utils.ToDateTime(Utils.ValidStrDateTime(startDate));
                o.EndDate           = Utils.ToDateTime(Utils.ValidStrDateTime(endDate));

                if (id > 0)
                    DiscountBo.Update(o);    
                
                responseData.Success  = true;
                responseData.Data     = null;
                responseData.TotalRow = 0;
            }
            catch (Exception ex)
            {
                responseData.Success = false;
                responseData.Message = ex.Message;
                Logger.WriteLog(Logger.LogType.Error, ex.Message);
            }
            return responseData;
        }

        private static ResponseData Delete()
        {
            var responseData = new ResponseData();

            var id = Utils.GetIdFromRequestString(Utils.DiscountId, 0);

            try
            {
                DiscountBo.Delete(id);

                responseData.Success    = true;
                responseData.Data       = null;
                responseData.TotalRow   = 0;
            }
            catch (Exception ex)
            {
                responseData.Success = false;
                responseData.Message = ex.Message;
                Logger.WriteLog(Logger.LogType.Error, ex.Message);
            }
            return responseData;
        }
    }
}
